package com.lanou.controller;

import com.lanou.bean.CpnInfo;
import com.lanou.bean.CpnUseRecord;
import com.lanou.bean.VipInfo;
import com.lanou.service.CpnInfoService;
import com.lanou.service.CpnUseRecordService;
import com.lanou.service.VipInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import sun.dc.pr.PRError;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("cpn")
public class CpnInfoController {

    @Autowired
    private CpnInfoService cpnInfoService;

    @Autowired
    private CpnUseRecordService cpnUseRecordService;

    @Autowired
    private VipInfoService vipInfoService;

    RestTemplate restTemplate=new RestTemplate();
    /**
     *  添加代金券信息
     * @return
     */
    @RequestMapping("addVipCpn.do")
    @ResponseBody
    public Map<String,String> addVipCpn(String cpnNo,Integer id){
        System.out.println("========"+cpnNo+id);
        //插入代金券

        //根据id查询会员信息
        VipInfo vipInfo = vipInfoService.selectByPrimaryKey(id);

        //根据代金券编号查询会员是否拥有这个代金券
        CpnInfo cpnInfo1 = cpnInfoService.selectCpnInfoByCpnNo(cpnNo);
        System.out.println("---------------"+cpnInfo1);
        Map<String,String>map=new HashMap<>();
        if (cpnInfo1!=null){
            map.put("result","false");
            return map;
        }else {
            //根据代金券的编号获取代金券信息
            MultiValueMap multiValueMap=new LinkedMultiValueMap();
            map=new HashMap<>();
            map.put("cpnNo",cpnNo);
            multiValueMap.setAll(map);
            CpnInfo cpnInfo = restTemplate.postForObject("http://10.90.87.207:8080/cpn/getCpnInfoByCpnNo.do", multiValueMap, CpnInfo.class);

            //向代金券列表列表插入代金券
            int ciresult = cpnInfoService.insertSelective(cpnInfo);
            System.out.println("向代金券表插入数据结果-----"+ciresult);
            //向中间表插入代金券关联

            //获取会员和代金券关联的中间表的最大id
            int maxId = cpnUseRecordService.selectMaxidInCpnUseRecord();
            System.out.println("获取到的最大id信息-------"+maxId);
            //创建中间表对象
            CpnUseRecord cpnUseRecord=new CpnUseRecord();
            cpnUseRecord.setId(maxId+1);
            cpnUseRecord.setCpnNo(cpnNo);
            cpnUseRecord.setVipNo(vipInfo.getVipNo());
            //插入关联关系
            int curresult = cpnUseRecordService.insertSelective(cpnUseRecord);
            System.out.println("向代金券会员关联表插入数据结果-----"+curresult);
            if(ciresult>0&&curresult>0){
                map.put("result","success");
                return map;
            }else {
                map.put("result","false");
                return map;
            }
        }
    }

}
